﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EShop.Core;
using EShop.Core.Caching;
using EShop.Core.Data;
using EShop.Core.Events;
using EShop.Core.Model.Store;

namespace EShop.Services.Store
{
    public class PayService : IManagePay
    {
        #region Constants
        private const string PaymentMethod_BY_ID_KEY = "EShop.PaymentMethod.id-{0}";
        private const string PaymentMethod_PATTERN_KEY = "EShop.PaymentMethod";
        #endregion

        #region Fields
        private readonly IRepository<PaymentMethod> _paymentmethodRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        //private readonly IDbContext _context;
        #endregion

        #region Ctor
        public PayService(IRepository<PaymentMethod> paymentmethodRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _paymentmethodRepository = paymentmethodRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        /// <summary>
        /// 添加支付方式
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddPay(PaymentMethod model)
        {

            if (model == null) throw new ExtException("NUll PaymentMethod Model");
            #region
            //检查是否有重复
            #endregion
            int rs = _paymentmethodRepository.Insert(model);
            if (rs > 0)
            {
                rs = 1;
                //event notification
                _eventPublisher.EntityInserted(model);

            }
            return rs;
        }
        /// <summary>
        /// 修改支付方式
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdatePay(PaymentMethod model)
        {
            if (model == null) throw new ExtException("NUll PaymentMethod Model");

            int rs = _paymentmethodRepository.Update(model);
            if (rs > 0)
            {
                //event notification
                _eventPublisher.EntityUpdated(model);

            }
            return rs > 0 ? true : false;
        }
        /// <summary>
        /// 删除支付方式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelPay(string id)
        {
            var model = _paymentmethodRepository.GetById(id);
            if (model == null)
                throw new ArgumentNullException("NUll Category Model");


            int r = _paymentmethodRepository.Delete(model);

            if (r > 0)
            {
                //cache
                _cacheManager.RemoveByPattern(PaymentMethod_PATTERN_KEY);

                //event notification
                _eventPublisher.EntityDeleted(model);
            }
            return r > 0 ? true : false;
        }
        /// <summary>
        /// 列表分页查询支付信息
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public IPagedList<PaymentMethod> GetAllPay(int pageIndex, int pageSize, string userName)
        {
            var m = _cacheManager.Get(PaymentMethod_PATTERN_KEY, () =>
            {
                var query = _paymentmethodRepository.Table;
                if (!string.IsNullOrEmpty(userName))
                    query = query.Where(c => c.PayUserName == userName);

                var unsortedPaymentMethod = query.ToList();

                //sort categories
                return unsortedPaymentMethod;
            });

            return new PagedList<PaymentMethod>(m, pageIndex, pageSize);
        }
        /// <summary>
        /// 获取单条支付方式
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PaymentMethod GetPayById(int id)
        {
            if (id <= 0)
                return null;
            //格式化缓存条件
            string key = string.Format(PaymentMethod_BY_ID_KEY, id);
            return _cacheManager.Get(key, () =>
            {
                var payment = _paymentmethodRepository.GetById(id);
                return payment;
            });
        }
        ///// <summary>
        ///// 获得支付信息通过条件
        ///// </summary>
        ///// <returns></returns>
        //public DataTable GetPayInfoByFiter(string fiter)
        //{

        //    DataTable dt = new DataTable();

        //    Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
        //    string str_sql = "  select  * from [tb_mc_pay] where " + fiter;
        //    try
        //    {

        //        dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
        //    }
        //    catch (Exception ex) { }

        //    return dt;
        //}
        /// <summary>
        /// 支付名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetPayNameById(int id)
        {
            return GetPayById(id).Name;
        }
    }
}
